package yhao.demo.aliyun.core;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;

import static yhao.demo.aliyun.ConfigFactory.*;

/**
 * 阿里云文件上传
 */
public class AliyunUploadComponent {
    @Autowired
    private Environment env;

    private long fileLimitSize = 10240;         //文件上传大小限制，单位k

    private String accessKey;
    private String secretKey;

    private BucketInfo fileBucket;

    @PostConstruct
    private void init() {
        String envFileLimitSize = env.getProperty(UPLOAD_FILE_SIZE);
        if (StringUtils.isNotEmpty(envFileLimitSize)) {
            fileLimitSize = Long.parseLong(envFileLimitSize);
        }
        accessKey = env.getProperty(ALIYUN_UPLOAD_ACCESSKEY);
        if (StringUtils.isBlank(accessKey)) {
            throw new RuntimeException("阿里云图片组件需要配置accessKey");
        }
        secretKey = env.getProperty(ALIYUN_UPLOAD_SECRETKEY);
        if (StringUtils.isBlank(secretKey)) {
            throw new RuntimeException("阿里云图片组件需要配置secretKey");
        }
        fileBucket = new BucketInfo(
                env.getProperty(ALIYUN_BUCKET_FILE_NAME), env.getProperty(ALIYUN_BUCKET_FILE_URL),
                env.getProperty(ALIYUN_BUCKET_FILE_ZONE));
    }

    public AliyunUploadResult fileUpload(AliyunUploadRequest request) {
        checkFileSizeLimit(request, fileLimitSize);
        return upload(request, fileBucket);
    }

    private AliyunUploadResult upload(AliyunUploadRequest request, BucketInfo bucket) {
        OSSClient ossClient = new OSSClient(bucket.endpoint, accessKey, secretKey);
        try{
            String key = request.getUploadFileName();
            byte[] content = request.getFileByte();
            PutObjectResult putResult = ossClient.putObject(bucket.name, key, new ByteArrayInputStream(content));
            AliyunUploadResult result = new AliyunUploadResult();
            result.setFileName(key);
            //putResult.getResponse()
            result.setUrl(bucket.url + "/" + request.getUploadFileName());
            result.setOriginalFileName(request.getOriginalFileName());
            return result;
        }finally {
            ossClient.shutdown();
        }
    }

    private void checkFileSizeLimit(AliyunUploadRequest request, long defaultLimitSize) {
        if (request.getFileSizeLimit() <= 0) {
            request.setFileSizeLimit(defaultLimitSize);
        }
        long fileSize = request.getFileByte().length / 1024;
        if (fileSize > request.getFileSizeLimit()) {
            throw new RuntimeException("上传文件大小超过最大限制");
        }
    }

    class BucketInfo {
        private String name;
        private String url;
        private String endpoint;

        public BucketInfo(String name, String url, String endpoint) {
            if (StringUtils.isBlank(name)) {
                throw new RuntimeException("阿里云图片组件需要bucket的name");
            }
            if (StringUtils.isBlank(url)) {
                throw new RuntimeException("阿里云图片组件需要bucket的url");
            }
            if (StringUtils.isBlank(endpoint)) {
                throw new RuntimeException("阿里云图片组件需要bucket的endpoint");
            }
            this.name = name;
            this.url = url;
            this.endpoint = endpoint;
        }
    }
}
